<?php
/**
 * RUBIK IT Solution & Development ltd.
 *
 * PROJECT		: Rubik Ultimate
 * @version 	: 1.1
 * COPYRIGHT	: 2008
 * ------------------------------------------------------
 * 
 * [GENERAL INFO OF THIS FILE] 
 *
 * @author			hoadn <hoadn@rubik.com.vn>
 * @access			[public/private]
 * @package		 [package name]
 *
 * Created on	:Sep 19, 2008
 */

// no direct access
defined ( '_FW_EXEC' ) or die ( 'Restricted access' );

RCoreImporter::cls('html.fieldInfo.BasicHtmlFieldInfo');

class FileHtmlFieldInfo extends BasicHtmlFieldInfo {
	
	var $_fileTypes = null;
	
	/**
	 * The list file type that supported
	 *
	 * @param string $fileTypeList '*.jpg, *.zip, *.pdf'
	 */
	function setSupportedFilteType($fileTypeList) {
		if (empty($fileTypeList) || strpos($fileTypeList, '*.*') !== false ) {
			$this->_fileTypes = null;
		}
		
		$arr = explode(',', $fileTypeList);
		$this->_fileTypes = array();
		foreach ($arr as $fileType) {
			$this->_fileTypes[] = strtolower(trim($fileType));
		}
	}
	
	/**
	 * Function for geting the html for previewing data
	 *
	 * @return string
	 */
	function buildHtmlPreviewData($extAttributes = '', $key=null, $dataType ='S') {
		$link = $this->getDownloadableLink();
		$lbl = $this->getFileName();
		$htmlData = "<a href=\"{$link}\">{$lbl}</a>";
		return $htmlData;
	}
	
	/**
	 * Get the link that can be download
	 *
	 * @return string
	 */
	function getDownloadableLink() {
		$link = $this->getValue();
		if (empty($link)) {
			return "#";
		}
		if (!(strpos($link, 'http') === 0 || strpos($link, '/') === 0)) {
			$link = SYS_ROOT_URL.$link;
		}
		return $link;
	}
	
	/**
	 * Get the file name
	 *
	 */
	function getFileName() {
		$link = $this->getValue();
		$fileName = substr($link, RStringHelper::lastIndexOf($link, '/'));
		if (empty($fileName)) {
			$fileName = substr($link, RStringHelper::lastIndexOf($link, '\\'));
		}
		return $fileName;
	}
	
	/**
	 * For checking if we need to create the filter header
	 * NOTE: please override in case special treat
	 *
	 * @return boolean
	 */
	function shouldCreateFilter() {
		return false;
	}
	
	/**
	 * Function for geting the html for inputing data
	 *
	 * @abstract 
	 * @return string
	 */
	function getHtmlInputData($dataType = '', $extAttribute = '') {
		$extAttribute .= $this->buildValidateHtmlAttribute($dataType);
		if ($this->_isContentOnly) {
			return $this->buildHtmlPreviewData($extAttribute, null, $dataType);
		}
		if ($this->_isReadOnly) {
			$readOnly = 'readonly="readonly"';
		}
		$value = htmlspecialchars($this->_value);
		$inputHtml = "<input {$readOnly} {$extAttribute} type=\"file\" name=\"{$this->_fieldName}\" id=\"{$this->_fieldName}\" value=\"{$value}\" />";
		return $inputHtml;
	}
	
}
?>
